package it.polimi.digitalstate.entities;

import it.polimi.digitalstate.dto.UserDTO;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.String;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.persistence.*;



/**
 * Entity implementation class for Entity: User
 *
 */
@Entity
@NamedQueries(value = { 
		@NamedQuery(name = "user_getByName", query = "select u from User u where u.username = :username" ),
		@NamedQuery(name = "user_getByUsername", query = "select u from User u where u.username = :username" ),
		@NamedQuery(name = "user_getByEmail", query = "select u from User u where u.email = :email" ),
		@NamedQuery(name = "user_getByCitizen", query = "select u from User u where u.citizen = :citizen" ),
		@NamedQuery(name = "user_getByID", query = "select u from User u where u.id = :id" )})
public class User implements Serializable , IEntity {

	@Id 
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	@Column(nullable = false, unique=true)
	private String username;
	@Column(nullable = false, unique=true)
	private String email;
	private String groupid;
	private String password;
	@JoinColumn(nullable=true)
	private Citizen citizen;
	
	private static final long serialVersionUID = 1L;

	public User() {
		super();
	}   
	public User(UserDTO dto) {
		username=dto.getUsername();
		email=dto.getEmail();
		groupid=dto.getGroupid();
			password=sha256(dto.getPassword());

	}
	public User(UserDTO dto, Citizen citizen) {
		username=dto.getUsername();
		email=dto.getEmail();
		groupid=dto.getGroupid();
			password=sha256(dto.getPassword());
			this.citizen=citizen;

	}
	private String sha256(String base) {
	    try{
	        MessageDigest digest = MessageDigest.getInstance("SHA-256");
	        byte[] hash = digest.digest(base.getBytes("UTF-8"));
	        StringBuffer hexString = new StringBuffer();

	        for (int i = 0; i < hash.length; i++) {
	            String hex = Integer.toHexString(0xff & hash[i]);
	            if(hex.length() == 1) hexString.append('0');
	            hexString.append(hex);
	        }

	        return hexString.toString();
	    } catch(Exception ex){
	       throw new RuntimeException(ex);
	    }
	}
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}   
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}   
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}   
	public String getGroupid() {
		return this.groupid;
	}

	public void setGroupid(String groupid) {
		this.groupid = groupid;
	}   
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public Citizen getCitizen() {
		return citizen;
	}
	public void setCitizen(Citizen citizen) {
		this.citizen = citizen;
	}
	public UserDTO getDTO(){
		return new UserDTO(id, username, email, password, groupid, (citizen == null)? null : citizen.getDTO());
	}
   
}
